<?php

namespace App\Http\Controllers\Blog;

use App\Http\Controllers\Controller;

use App\Http\Requests\LinkSaveRequest;
use App\Models\Link;
use App\Http\Responses\Response;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class LinkController extends Controller
{
    /**
     * 前台查询友情链接
     * @return JsonResponse
     */
    public function list_sort()
    {
        $links = Link::where('link_status', '=', 0)->orderby('link_weight', 'desc')->get();
        return Response::resource_success($links);
    }

    public function save(LinkSaveRequest $request)
    {

        $cleanData = $request->validated();

        $link = Link::create([
            "link_name" => $cleanData["name"],
            "link_url" => $cleanData["url"],
            "link_weight" => 0,
            "link_create_time" => date("Y-m-d H:i:s"),
            "link_status" => 1
        ]);
        try{
            $link->save();
        } catch (Exception $exception){
            return Response::insert_error();
        }

        return Response::insert_success($link);
    }

}
